/*
 * @Description:
 * @Version: 1.0
 * @Autor: laikt
 * @Date: 2020-12-30 13:54:19
 * @LastEditors: laikt
 * @LastEditTime: 2020-12-30 16:27:53
 */
import Vue from 'vue'
import VueRouter from 'vue-router'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
import utils from '@/utils'
import Home from '../views/Home.vue'

Vue.use(VueRouter)
const routes = [
    {
        path: '/',
        name: 'Home',
        component: Home
    },
    {
        path: '/about',
        name: 'About',
        // route level code-splitting
        // this generates a separate chunk (about.[hash].js) for this route
        // which is lazy-loaded when the route is visited.
        component: () =>
            import(/* webpackChunkName: "about" */ '../views/About.vue')
    }
]

// 导出路由 在 main.js 里使用
const router = new VueRouter({
    scrollBehavior: () => ({ y: 0 }),
    routes
})

router.beforeEach(async (to, from, next) => {
    NProgress.start()
    try {
        next()
    } catch (error) {
        next(false)
    }
    NProgress.done()
})

router.afterEach(to => {
    // 进度条
    NProgress.done()
    // 更改标题
    utils.title(to.meta.title)
})

export default router
